package com.skateboard.reelviewtest;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.skateboard.reelviewtest.adapter.Adapter3D;
import com.skateboard.reelviewtest.rotate.Rotate3dAnimation;
import com.skateboard.reelviewtest.utils.SimpleUtils;

public class ListActivity extends AppCompatActivity implements Adapter3D.OnItemClickLitener {
    RecyclerView recyclerView;
    RelativeLayout rootView;
    Rotate3dAnimation rotateAnimation1;
    Rotate3dAnimation rotateAnimation2;
    ScaleAnimation scaleAnimation;
    RelativeLayout childView = null;
    RelativeLayout layout_imageview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        recyclerView=findViewById(R.id.recyclerView);
        rootView=findViewById(R.id.rootView);
        layout_imageview=findViewById(R.id.layout_imageview);
        Adapter3D adapter3D=new Adapter3D();
        recyclerView.setAdapter(adapter3D);

        recyclerView.setLayoutManager(new GridLayoutManager(this,2));

        adapter3D.setmOnItemClickLitener(this);

    }


    @Override
    public void onItemClick(View view, int position) {

        doAnimation(view);
        //doAnimationView2Image(view);
    }

    public void testView2Image(View view){
        Bitmap cacheBitmapFromView = SimpleUtils.getCacheBitmapFromView(view);
        //testImage.setImageBitmap(cacheBitmapFromView);
    }
    public void doAnimation(View view){
        RelativeLayout relativeLayout= (RelativeLayout) view;
        for(int i=0;i<relativeLayout.getChildCount();i++){
            View child=relativeLayout.getChildAt(i);
            if(child.getId()==R.id.item_1){
                childView= (RelativeLayout) child;
            }
        }
        if(childView==null){
            return;
        }

        final int[] location=new int[2];
        childView.getLocationOnScreen(location);
        final int[]recycleLocation=new int[2];
        recyclerView.getLocationOnScreen(recycleLocation);



//        if(childView.getParent()!=null){
//            ((RelativeLayout)(childView.getParent())).removeView(childView);
//        }

        RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) layout_imageview.getLayoutParams();
        layoutParams.leftMargin=location[0];
        layoutParams.topMargin=location[1]-recycleLocation[1];
        layout_imageview.setLayoutParams(layoutParams);


//        childView.setLayoutParams(layoutParams);
//        rootView.addView(childView);

        int i=rootView.getHeight()/2;
        int j= (int) (SimpleUtils.dp2px(this,250)/2);
        float k=location[1]-recycleLocation[1];
        int yTrans= (int) (i-j-k);
        Log.e("ssss",yTrans+"====result====");
        Log.e("ssss","top:"+childView.getTop()+"========left:"+childView.getLeft()+"======x:"+childView.getX()+"y:"+childView.getY());
        int xTrans= (int) (rootView.getWidth()/2-SimpleUtils.dp2px(this,150)/2-location[0]);


//        //属性动画 平移
//        ObjectAnimator translationY = ObjectAnimator.ofFloat(layout_imageview, "translationY", yTrans);
//
//
//        ObjectAnimator translationX = ObjectAnimator.ofFloat(layout_imageview, "translationX", xTrans);
//
//        AnimatorSet set=new AnimatorSet();
//        set.play(translationX).with(translationY);
//        set.setDuration(0)

        TranslateAnimation translateAnimation = new TranslateAnimation(
                //X轴初始位置
                0,
                //X轴移动的结束位置
                xTrans,
                //y轴开始位置
                0,
                //y轴移动后的结束位置
                yTrans);
        translateAnimation.setDuration(1000);
        //如果fillAfter的值为真的话，动画结束后，控件停留在执行后的状态
        translateAnimation.setFillAfter(true);
        //将AlphaAnimation这个已经设置好的动画添加到 AnimationSet中
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) layout_imageview.getLayoutParams();
                params.topMargin=rootView.getHeight()/2-layout_imageview.getHeight()/2;
                params.leftMargin=rootView.getWidth()/2-layout_imageview.getWidth()/2;
                layout_imageview.setLayoutParams(params);
                AnimationSet animationSet=new AnimationSet(true);
                animationSet.addAnimation(rotateAnimation1);
                animationSet.addAnimation(scaleAnimation);
                animationSet.setDuration(3000);
                animationSet.setFillAfter(true);
                layout_imageview.startAnimation(animationSet);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        //启动动画
        layout_imageview.startAnimation(translateAnimation);
        layout_imageview.setVisibility(View.VISIBLE);
        childView.setVisibility(View.GONE);
        //属性动画
        //平移动画
//        ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(childView,"translationX", xTrans);
//        //objectAnimatorX.setDuration(1000);
//
//
//        ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(childView,"translationY", yTrans);
//        //objectAnimatorY.setDuration(1000);
//
//        final AnimatorSet animatorSet=new AnimatorSet();
//        animatorSet.play(objectAnimatorX).with(objectAnimatorY);
//        animatorSet.setDuration(1000);
//        animatorSet.addListener(new AnimatorListenerAdapter() {
//            @Override
//            public void onAnimationEnd(Animator animation) {
//
//            }
//        });
        //animatorSet.start();
        //翻转
        //从0到90度，顺时针旋转视图，此时reverse参数为true，达到90度时动画结束时视图变得不可见，
        float centerX=SimpleUtils.dp2px(this,150)/2;
        float centerY=SimpleUtils.dp2px(this,250)/2;
        float depthZ=1.0f;
        rotateAnimation1 = new Rotate3dAnimation(this,0, 360, centerX, centerY, depthZ, true);
//        rotateAnimation1.setDuration(3000);
//        rotateAnimation1.setFillAfter(true);
//        rotateAnimation1.setInterpolator(new LinearInterpolator());


//        rotateAnimation2 = new Rotate3dAnimation(this,90, 180, centerX, centerY, depthZ, true);
//        rotateAnimation2.setDuration(1000);
//        rotateAnimation2.setFillAfter(true);
//        rotateAnimation2.setInterpolator(new LinearInterpolator());

        scaleAnimation = new ScaleAnimation(1F,2f,
                1F,2f,
                Animation.RELATIVE_TO_SELF,0.5f,
                Animation.RELATIVE_TO_SELF,0.5f);

    }
    public void doAnimationView2Image(final View view){
        Bitmap cacheBitmapFromView = SimpleUtils.getCacheBitmapFromView(view);
        if(cacheBitmapFromView==null){
            return;
        }
        final ImageView imageView=new ImageView(this);
        imageView.setImageBitmap(cacheBitmapFromView);
        LinearLayout.LayoutParams imageParams= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        imageParams.width=view.getWidth();
        imageParams.height=view.getHeight();

        final int[] location=new int[2];
        view.getLocationOnScreen(location);
        final int[]recycleLocation=new int[2];
        recyclerView.getLocationOnScreen(recycleLocation);
        imageParams.leftMargin=location[0];
        imageParams.topMargin=location[1]-recycleLocation[1];
        imageView.setLayoutParams(imageParams);
        rootView.addView(imageView);

        final int i=rootView.getHeight()/2;
        final int j=view.getHeight()/2;
        final float k=location[1]-recycleLocation[1];//
        float yTrans= (int) (i-j-k-100);
        //float yTrans= SimpleUtils.getScreenHight(this)/2-view.getHeight()/2-location[1];
        Log.e("ssss",yTrans+"====result====");
        Log.e("ssss","top:"+imageView.getTop()+"========left:"+imageView.getLeft()+"======x:"+imageView.getX()+"y:"+imageView.getY());

        int xTrans=rootView.getWidth()/2-view.getWidth()/2-location[0];
        TranslateAnimation translateAnimation = new TranslateAnimation(
                //X轴初始位置
                0,
                //X轴移动的结束位置
                xTrans,
                //y轴开始位置
                0,
                //y轴移动后的结束位置
                yTrans);
        translateAnimation.setDuration(1000);
        //如果fillAfter的值为真的话，动画结束后，控件停留在执行后的状态
        translateAnimation.setFillAfter(true);
        //将AlphaAnimation这个已经设置好的动画添加到 AnimationSet中
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) imageView.getLayoutParams();
                params.topMargin= (int) (i-j-100);
                params.leftMargin=rootView.getWidth()/2-imageView.getWidth()/2;
                imageView.setLayoutParams(params);
                AnimationSet animationSet=new AnimationSet(true);
                animationSet.addAnimation(rotateAnimation1);
                animationSet.addAnimation(scaleAnimation);
                animationSet.setDuration(3000);
                animationSet.setFillAfter(true);
                imageView.startAnimation(animationSet);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        //启动动画
        imageView.startAnimation(translateAnimation);
        //属性动画
        //平移动画
//        ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(childView,"translationX", xTrans);
//        //objectAnimatorX.setDuration(1000);
//
//
//        ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(childView,"translationY", yTrans);
//        //objectAnimatorY.setDuration(1000);
//
//        final AnimatorSet animatorSet=new AnimatorSet();
//        animatorSet.play(objectAnimatorX).with(objectAnimatorY);
//        animatorSet.setDuration(1000);
//        animatorSet.addListener(new AnimatorListenerAdapter() {
//            @Override
//            public void onAnimationEnd(Animator animation) {
//
//            }
//        });
        //animatorSet.start();
        //翻转
        //从0到90度，顺时针旋转视图，此时reverse参数为true，达到90度时动画结束时视图变得不可见，
        float centerX=view.getWidth()/2;
        float centerY=view.getHeight()/2;
        float depthZ=1.0f;
        rotateAnimation1 = new Rotate3dAnimation(this,0, 360, centerX, centerY, depthZ, true);
//        rotateAnimation1.setDuration(3000);
//        rotateAnimation1.setFillAfter(true);
//        rotateAnimation1.setInterpolator(new LinearInterpolator());


//        rotateAnimation2 = new Rotate3dAnimation(this,90, 180, centerX, centerY, depthZ, true);
//        rotateAnimation2.setDuration(1000);
//        rotateAnimation2.setFillAfter(true);
//        rotateAnimation2.setInterpolator(new LinearInterpolator());

        scaleAnimation = new ScaleAnimation(1F,1.8f,
                1F,1.8f,
                Animation.RELATIVE_TO_SELF,0.5f,
                Animation.RELATIVE_TO_SELF,0.5f);

    }
}